﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Luna.WPF.ApplicationFramework;
using Caliburn.Core.Metadata;
using System.ComponentModel;
using Caliburn.PresentationFramework;
using XWord.Fundamental.Domain;
using XWord.WPF.ApplicationFramework;
namespace XWord.GUI.Presenters.Fundamental
{
    [Singleton(typeof(EtymaCategoryExplorerPresenter))]
    public class EtymaCategoryExplorerPresenter : DockingPresenter
    {
        protected override void OnInitialize()
        {
            EtymaLibraryPresenter.PropertyChanged += new PropertyChangedEventHandler(EtymaLibraryExplorerPresenter_PropertyChanged);
            base.OnInitialize();
        }

        void EtymaLibraryExplorerPresenter_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "AllEtymaLibraryList")
            {
                GetPreFix();
                GetSuffix();
                GetEtymonList();
                EtymaLibraryPresenter.PropertyChanged -= EtymaLibraryExplorerPresenter_PropertyChanged;
            }
        }

        private List<EtymaMessage> _etymaMessageList;

        private void GetEtymaMessage()
        {
            if (_etymaMessageList == null)
            {
                _etymaMessageList = new List<EtymaMessage>();
                var list = EtymaLibraryPresenter.AllEtymaLibraryList;
                foreach (var item in list)
                {
                    _etymaMessageList.AddRange(EtymaLibraryExplorerPresenter.ParseText(item.Name));
                }
            }
        }

        public void GetPreFix()
        {
            GetEtymaMessage();
            PrefixEtymaList=_etymaMessageList.Where(e => (e.EtymaType & EtymaType.PreFix) == EtymaType.PreFix).ToObservableCollection();
        }

        public void GetSuffix()
        {
            GetEtymaMessage();
            SuffixEtymaList = _etymaMessageList.Where(e => (e.EtymaType & EtymaType.Suffix) == EtymaType.Suffix).ToObservableCollection();
        }

        public void GetEtymonList()
        {
            GetEtymaMessage();
            EtymonList = _etymaMessageList.Where(e => (e.EtymaType & EtymaType.Etymon) == EtymaType.Etymon).ToObservableCollection();
        }

        public IObservableCollection<EtymaMessage> _prefixEtymaList;
        public IObservableCollection<EtymaMessage> PrefixEtymaList
        {
            get { return _prefixEtymaList ?? (_prefixEtymaList = new BindableCollection<EtymaMessage>()); }
            set
            {
                _prefixEtymaList = value;
                NotifyOfPropertyChange("PrefixEtymaList");
            }
        }

        public IObservableCollection<EtymaMessage> _etymonList;
         public IObservableCollection<EtymaMessage> EtymonList
        {
            get { return _etymonList ?? (_etymonList = new BindableCollection<EtymaMessage>()); }
            set
            {
                _etymonList = value;
                NotifyOfPropertyChange("EtymonList");
            }
        }


        

        public IObservableCollection<EtymaMessage> _suffixEtymaList;
        public IObservableCollection<EtymaMessage> SuffixEtymaList
        {
            get { return _suffixEtymaList ?? (_suffixEtymaList = new BindableCollection<EtymaMessage>()); }
            set
            {
                _suffixEtymaList = value;
                NotifyOfPropertyChange("SuffixEtymaList");
            }
        }

        public EtymaLibraryExplorerPresenter EtymaLibraryPresenter { get; set; }
    }
}
